#ifndef COMMAND_H
#define COMMAND_H
#include <stdio.h>
#include <string>


namespace Utils {

class CommandHelper {
public:
    static std::string ExecCommand(const std::string& strCmd, int nCntFilterTag = 1)
    {
        std::string strOutput;
        #ifdef WIN32
            FILE* pFile = _popen(strCmd.c_str(), "r");
        #else
            FILE* pFile = popen(strCmd.c_str(), "r");
        #endif // WIN32
        if(pFile) {
            char szBuffer[1024] =   { 0 };
            memset(szBuffer,'\0', 1024);
            while (fgets(szBuffer, 200, pFile) != NULL) {
                strOutput += szBuffer;
            }
            #ifdef WIN32
                _pclose(pFile);
            #else
                pclose(pFile);
            #endif
        }
        while(nCntFilterTag-- > 0) {
            if(!strOutput.empty() && strOutput.back() == '\n') {
                strOutput.pop_back();
            }
        }
        return strOutput;
    }
};

}
#endif // COMMAND_H
